﻿using System;
using System.Windows.Data;
using System.Text;
namespace ReflectionStudio.Controls
{
    /// <summary>
    /// Converter : Boolean to System.Windows.Visibility and not revert
    /// </summary>
    [ValueConversion(typeof(string), typeof(System.Windows.Visibility))]
    public class GB2312Convert8859P1 : IValueConverter
    {
        /// <summary>
        /// IValueConverter.Convert
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return "";
            if (value.GetType().ToString().Contains("DBNull"))
                return "";
            return System.Text.Encoding.Default.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(value.ToString()));
        }

        /// <summary>
        /// IValueConverter.ConvertBack (not implemented)
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] b;
            string filevalue = "";
            if (value == null)
                return "";
            b = Encoding.GetEncoding("gb2312").GetBytes(value as string);
            filevalue = Encoding.GetEncoding("ISO-8859-1").GetString(b);
            return filevalue;
        }
    }
}
